package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"
)

/*
===生产者消费者模型之同步无缓冲模型===
  - 使用无缓冲通道，则生产一个消费一个，且生产与消费不能是同一个goruntine
*/
func main() {
	ch := make(chan int)
	go producer(ch)
	consumer(ch)
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	fmt.Printf("quit (%v)\n", <-sig)
}

func producer(ch chan<- int) {
	for i := 0; i < 10; i++ {
		ch <- i * i
		fmt.Println("生产：", i*i)
	}
	close(ch)
}
func consumer(ch <-chan int) {
	for num := range ch {
		fmt.Println("消费：", num)
		time.Sleep(time.Microsecond)
	}
}
